package security

import (
	"context"
	"database/sql"

	dbmodel "gitee.com/hp-huiw/family-expense-api/internal/db/model"
	"gitee.com/hp-huiw/family-expense-api/internal/repository"
	"gitee.com/hp-huiw/my-go-core/security"
	"github.com/samber/lo"
)

type SecurityRequestMapQuery struct {
	db *sql.DB
}

func NewSecurityRequestMapQuery(db *sql.DB) *SecurityRequestMapQuery {
	return &SecurityRequestMapQuery{db: db}
}

func (r *SecurityRequestMapQuery) QueryAll(ctx context.Context) ([]security.RequestMap, error) {
	requestMapRepository := repository.NewRequestMapRepository(r.db)
	query, err := requestMapRepository.Query(ctx)

	if err != nil {
		return nil, err
	}

	requestMaps := lo.Map(query, func(item dbmodel.RequestMap, _ int) security.RequestMap {
		return security.RequestMap{
			ID:              security.NewIDField(item.ID),
			ConfigAttribute: item.ConfigAttribute,
			HTTPMethod:      item.HTTPMethod,
			URL:             item.URL,
		}
	})

	return requestMaps, nil
}
